<template>
  <div class="login-mobile">
    <el-form ref="ruleFormRef" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" clearable />
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input
          v-model="form.code"
          clearable
          style="width: 160px; margin-right: 20px"
        />
        <el-button type="primary" @click="submit(ruleFormRef)"
          >接收验证码</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';

export default defineComponent({
  setup() {
    const ruleFormRef = ref<FormInstance>();
    const form = reactive({
      mobile: '',
      code: '',
    });
    const rules = reactive<FormRules>({
      mobile: [
        {
          required: true,
          message: '请输入手机号！',
          trigger: 'blur',
        },
      ],
      code: [
        {
          required: true,
          message: '请输入验证码！',
          trigger: 'blur',
        },
        {
          min: 4,
          max: 4,
          message: '请输入4位验证码！',
          trigger: 'blur',
        },
      ],
    });

    const getCode = () => {
      console.log('接收验证码');
    };

    const submit = async (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      await formEl.validate((valid, fields) => {
        if (valid) {
          console.log('submit!');
        } else {
          console.log('error submit!', fields);
        }
      });
    };

    return {
      ruleFormRef,
      form,
      rules,
      getCode,
      submit,
    };
  },
});
</script>

<style scoped></style>
